package com.px.user.mapper;

import com.px.user.entity.RedPack;
import com.px.user.entity.RedPackGrab;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface RedPackMapper {

    /**
     * 发红包
     * @param redPack
     */
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into red_pack (creatorId,money,amount,roomId,category) values (#{creatorId},#{money},#{amount},#{roomId},#{category})")
    void insert(RedPack redPack);

    @Select("select * from red_pack where creatorId=#{creatorId} and createTime between #{start} and #{end}")
    List<RedPack> findByUserId(Map map);

    @Select({"<script>",
            "select red_pack.*,user.nickname from red_pack left join user on red_pack.creatorId=user.id",
            "<where>",
            " <if test='id != null'>and id = #{id}</if>",
            " <if test='creatorId != null'>and creatorId = #{creatorId}</if>",
            " <if test='roomId != null'>and roomId = #{roomId}</if>",
            " <if test='category != null'>and category = #{category}</if>",
            " <if test='qstart != null'>and red_pack.createTime >= #{qstart}</if>",
            " <if test='qend != null'>and red_pack.createTime <![CDATA[<=]]> #{qend}</if>",
            "</where>",
            "order by ${orderField} desc limit #{startIndex},#{limit}",
            "</script>"})
    List<RedPack> paging(RedPack redPack);

    @Select({"<script>",
            "select count(1) from red_pack",
            "<where>",
            " <if test='id != null'>and id = #{id}</if>",
            " <if test='creatorId != null'>and creatorId = #{creatorId}</if>",
            " <if test='roomId != null'>and roomId = #{roomId}</if>",
            " <if test='category != null'>and category = #{category}</if>",
            " <if test='qstart != null'>and createTime >= #{qstart}</if>",
            " <if test='qend != null'>and createTime <![CDATA[<=]]> #{qend}</if>",
            "</where>",
            "</script>"})
    Integer pagingCount(RedPack redPack);

    /**
     * 抢红包记录
     * @param redPackGrab
     */
    @Insert("insert into red_pack_grab (redPackId,toUserId,money) values (#{redPackId},#{toUserId},#{money})")
    void insertGrab(RedPackGrab redPackGrab);

    @Select("select * from red_pack_grab where toUserId=#{toUserId} and createTime between #{start} and #{end}")
    List<RedPackGrab> findGrabByUserId(Map map);

    @Select({"<script>",
            "select red_pack_grab.*,red_pack.creatorId as fromUserId,u2.isRobot,",
            "u1.nickname as fromNickname,u2.nickname as toNickname from red_pack_grab",
            " inner join red_pack on red_pack_grab.redPackId=red_pack.id",
            " left join user as u1 on red_pack.creatorId=u1.id",
            " left join user as u2 on red_pack_grab.toUserId=u2.id",
            "<where>",
            " <if test='id != null'>and id = #{id}</if>",
            " <if test='redPackId != null'>and redPackId = #{redPackId}</if>",
            " <if test='toUserId != null'>and toUserId = #{toUserId}</if>",
            " <if test='qstart != null'>and red_pack_grab.createTime >= #{qstart}</if>",
            " <if test='qend != null'>and red_pack_grab.createTime <![CDATA[<=]]> #{qend}</if>",
            "</where>",
            "order by ${orderField} desc limit #{startIndex},#{limit}",
            "</script>"})
    List<RedPackGrab> grabPaging(RedPackGrab redPackGrab);

    @Select({"<script>",
            "select count(1) from red_pack_grab",
            " inner join red_pack on red_pack_grab.redPackId=red_pack.id",
            "<where>",
            " <if test='id != null'>and id = #{id}</if>",
            " <if test='redPackId != null'>and redPackId = #{redPackId}</if>",
            " <if test='toUserId != null'>and toUserId = #{toUserId}</if>",
            " <if test='qstart != null'>and red_pack_grab.createTime >= #{qstart}</if>",
            " <if test='qend != null'>and red_pack_grab.createTime <![CDATA[<=]]> #{qend}</if>",
            "</where>",
            "</script>"})
    Integer grabPagingCount(RedPackGrab redPackGrab);
}
